package com.kalix.jyksh.proint.biz;


import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;
import com.kalix.ksh.proint.api.biz.ICpuHistogramTitleBeanService;
import com.kalix.ksh.proint.api.dao.ICpuHistogramTitleBeanDao;
import com.kalix.ksh.proint.api.model.PostRequestUtil;
import com.kalix.jyksh.proint.entities.CpuHistogramTitleBean;
import com.kalix.jyksh.proint.entities.MemoryHistogramTitleBean;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Wanghw on 2018/10/9.
 * cpu top5
 *
 * {"metricses":[{"metricsDefPath":"MetricsDefinition.mo=\"NetworkDevice\",name=\"CPU\"","arguments":"","sortable":true}],"filters":[{"grades":[1,2,3],"pdmEngine":"defaultEngine","moType":"NetworkDevice","moFilter":"1=1","permissionMoType":"NetworkDevice","permissionMoFilter":"1=1"}]}{"metricses":[{"metricsDefPath":"MetricsDefinition.mo=\"NetworkDevice\",name=\"CPU\"","arguments":"","sortable":true}],"filters":[{"grades":[1,2,3],"pdmEngine":"defaultEngine","moType":"NetworkDevice","moFilter":"1=1","permissionMotype":"NetworfDevice","permissionMoFilter":"1=1"}]}
 *
 *{"metricses":[{"metricsDefPath":"MetricsDefinition.mo=\"NetworkDevice\",name=\"MEM\"","arguments":"","sortable":true}],"filters":[{"grades":[1,2,3],"pdmEngine":"defaultEngine","moType":"NetworkDevice","moFilter":"1=1","permissionMoType":"NetworkDevice","permissionMoFilter":"1=1"}]}{"metricses":[{"metricsDefPath":"MetricsDefinition.mo=\"NetworkDevice\",name=\"MEM\"","arguments":"","sortable":true}],"filters":[{"grades":[1,2,3],"pdmEngine":"defaultEngine","moType":"NetworkDevice","moFilter":"1=1","permissionMotype":"NetworfDevice","permissionMoFilter":"1=1"}]}
 *
 */
public class CpuHistogramTitleBeanServiceImpl extends GenericBizServiceImpl<ICpuHistogramTitleBeanDao, CpuHistogramTitleBean> implements ICpuHistogramTitleBeanService {
    public CpuHistogramTitleBeanServiceImpl(){
        super.init(ICpuHistogramTitleBeanDao.class.getName());
    }
    public static String wsIp = (String) ConfigUtil.getConfigProp("FREEBOARD_IP","config.ksh.portconf");
    public static String wsPort =(String)ConfigUtil.getConfigProp("FREEBOARD_PORT","config.ksh.portconf");

    /**
     * cpu top数据返回
     * @return
     */
    @Override
    public String getCPUTop() {
//        wsIp = "192.168.31.28";
//        wsPort = "8080";
        String sql = "select li from CpuHistogramTitleBean li";
        List<CpuHistogramTitleBean> list = dao.find(sql);
        int num = Integer.parseInt(list.get(0).getMax());
        String strJson = "{\"metricses\":[{\"metricsDefPath\":\"MetricsDefinition.mo=\\\"NetworkDevice\\\",name=\\\"MEM\\\"\",\"arguments\":\"\",\"sortable\":true}],\"filters\":[{\"grades\":[1,2,3],\"pdmEngine\":\"defaultEngine\",\"moType\":\"NetworkDevice\",\"moFilter\":\"1=1\",\"permissionMoType\":\"NetworkDevice\",\"permissionMoFilter\":\"1=1\"}]}{\"metricses\":[{\"metricsDefPath\":\"MetricsDefinition.mo=\\\"NetworkDevice\\\",name=\\\"MEM\\\"\",\"arguments\":\"\",\"sortable\":true}],\"filters\":[{\"grades\":[1,2,3],\"pdmEngine\":\"defaultEngine\",\"moType\":\"NetworkDevice\",\"moFilter\":\"1=1\",\"permissionMotype\":\"NetworfDevice\",\"permissionMoFilter\":\"1=1\"}]}";
//        String json = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/api/v1/datapoints/query",strJson);
//        String strJson = "";
        String uri = "http://"+wsIp+":"+wsPort+"/v2/metrics/topN?top="+num+"&role=admin";
        String json = PostRequestUtil.HttpPostWithJson(uri,strJson);
        JSONObject jsonObject = new JSONObject(json);
        JSONArray js = jsonObject.getJSONArray("body");
        JSONObject jObject = new JSONObject(); // 返回数据
        JSONArray dataList = new JSONArray();
        JSONArray pList = new JSONArray();
        JSONArray xAxisData = new JSONArray();
        JSONArray titleData = new JSONArray();
        JSONObject dataLists = new JSONObject();

        for (int i = 0; i < num; i++) {
            JSONObject jj = js.getJSONObject(i);
            JSONObject permissionMo = jj.getJSONObject("permissionMo");
            String deviceName = permissionMo.get("deviceName").toString();
            JSONArray topNMetricsList = jj.getJSONArray("topNMetricsList");
            JSONObject tt = topNMetricsList.getJSONObject(0);
            String value = tt.get("value").toString();
            dataList.put(value);
            pList.put(deviceName);
            xAxisData.put(deviceName);
            titleData.put(list.get(0).getTitle());
        }
        for (int i = 0; i <5 ; i++) {
            dataLists.put(""+i, dataList);
        }
        JSONObject yAxis = new JSONObject();
        yAxis.put("name",list.get(0).getYaxisname());
        yAxis.put("max",5);

        jObject.put("pList",pList);
        jObject.put("dataList",dataLists);
        jObject.put("xAxisData",xAxisData);
        jObject.put("yAxis",yAxis);
        jObject.put("xAxisName",list.get(0).getAxisname());
        jObject.put("titleData",titleData);


        return jObject.toString();
    }


    /**
     * 内存top5
     * @return
     */
    @Override
    public String getMemoryTop() {
//        wsIp = "192.168.31.28";
//        wsPort = "8080";
        String sql = "select li from MemoryHistogramTitleBean li";
        List<MemoryHistogramTitleBean> list = dao.find(sql);
        int num = Integer.parseInt(list.get(0).getMax());
//        String json = PostRequestUtil.HttpPostWithJson("http://"+wsIp+":"+wsPort+"/api/v1/datapoints/query",strJson);
        String strJson = "{\"metricses\":[{\"metricsDefPath\":\"MetricsDefinition.mo=\\\"NetworkDevice\\\",name=\\\"MEM\\\"\",\"arguments\":\"\",\"sortable\":true}],\"filters\":[{\"grades\":[1,2,3],\"pdmEngine\":\"defaultEngine\",\"moType\":\"NetworkDevice\",\"moFilter\":\"1=1\",\"permissionMoType\":\"NetworkDevice\",\"permissionMoFilter\":\"1=1\"}]}{\"metricses\":[{\"metricsDefPath\":\"MetricsDefinition.mo=\\\"NetworkDevice\\\",name=\\\"MEM\\\"\",\"arguments\":\"\",\"sortable\":true}],\"filters\":[{\"grades\":[1,2,3],\"pdmEngine\":\"defaultEngine\",\"moType\":\"NetworkDevice\",\"moFilter\":\"1=1\",\"permissionMotype\":\"NetworfDevice\",\"permissionMoFilter\":\"1=1\"}]}";
        String uri = "http://"+wsIp+":"+wsPort+"/v2/metrics/topN?top="+num+"&role=admin";
        String json = PostRequestUtil.HttpPostWithJson(uri,strJson);
        JSONObject jsonObject = new JSONObject(json);
        JSONArray js = jsonObject.getJSONArray("body");
        JSONObject jObject = new JSONObject(); // 返回数据
        JSONArray dataList = new JSONArray();
        JSONArray pList = new JSONArray();
        JSONArray xAxisData = new JSONArray();
        JSONArray titleData = new JSONArray();
        JSONObject dataLists = new JSONObject();

        for (int i = 0; i < num; i++) {
            JSONObject jj = js.getJSONObject(i);
            JSONObject permissionMo = jj.getJSONObject("permissionMo");
            String deviceName = permissionMo.get("deviceName").toString();
            JSONArray topNMetricsList = jj.getJSONArray("topNMetricsList");
            JSONObject tt = topNMetricsList.getJSONObject(0);
            String value = tt.get("value").toString();
            dataList.put(value);
            pList.put(deviceName);
            xAxisData.put(deviceName);
            titleData.put(list.get(0).getTitle());

        }
        for (int i = 0; i <5 ; i++) {
            dataLists.put(""+i, dataList);
        }

        JSONObject yAxis = new JSONObject();
        yAxis.put("name",list.get(0).getYaxisname());
        yAxis.put("max",5);

        jObject.put("pList",pList);
        jObject.put("dataList",dataLists);
        jObject.put("xAxisData",xAxisData);
        jObject.put("yAxis",yAxis);
        jObject.put("xAxisName",list.get(0).getAxisname());
        jObject.put("titleData",titleData);


        return jObject.toString();
    }
}
